package com.redis.jedis;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class RedisServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(4000);
            while (true) {
                Socket socket = serverSocket.accept();
                // 开启线程
                new Thread(() -> {
                    try (InputStream inputStream = socket.getInputStream()) {
                        while (true) {
                            byte[] bytes = new byte[1024];
                            int read = inputStream.read(bytes);
                            if (read == -1) {
                                break;
                            }
                            String request = new String(bytes, 0, read);
                            System.out.println(request);
                        }
                        if (socket.isClosed()){
                            socket.shutdownInput();
                            socket.shutdownOutput();
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
